<?php
/*
 * @开发工具: JetBrains PhpStorm.
 * @文件名：IJPushCache.class.php
 * @类功能: 极光推送类
 * @开发者: zc
 * @开发时间： 14-10-28
 * @版本：version 1.0
 */
namespace app\common\ext;

use JPush\Client as JPush;
use JPush\Exceptions\JPushException;
use JPush\Exceptions\APIConnectionException;
use JPush\Exceptions\APIRequestException;

require 'JPushApi/autoload.php';

class IIJPush {

    //静态化缓存列表
    private static $pr_app_key = "f4d049a9a1894e8963fd02c3";
    private static $pr_master_secret = "b8404841983b3442a0216976";

    /*
     * @功能 推送
     * @param string $title 标题
     * @param string $content 内容
     * @param string $alias 别名
     * @param string $tag 标签
     * @param string $param 参数
     * @开发者：cxl
     * @return bool
     */
    public static function Push($title,$content,$alias=null,$tag=array(),$param=array()) {
        //实例化对象
        $push = new JPush(self::$pr_app_key, self::$pr_master_secret);

        //设置消息参数
        $ios = array("sound"=> "default", "badge"=> "+1", "extras"=> $param);
        $android = array("title"=>$title, "builder_id"=> 1, "extras"=> $param);
        $iosAlert = array('title'=>$title, 'body'=>$content);


        //检测异常
        try {
            //判断发送方式
            if(!empty($alias)){
                //发送推送
                $alias = strval($alias);
                $response = $push->push()
                    ->setPlatform("all")
                    ->addAlias($alias)
                    ->iosNotification($iosAlert,$ios)
                    ->androidNotification($content,$android)
                    ->send();
            }else{
                //判断是否为标签推送
                if(!empty($tag) && is_array($tag) && count($tag) > 0) {
                    //发送推送
                    $response = $push->push()
                        ->setPlatform("all")
                        ->addTag($tag)
                        ->iosNotification($iosAlert,$ios)
                        ->androidNotification($content,$android)
                        ->send();
                }else{
                    //发送推送
//                    $response = $push->push()
//                        ->setPlatform("all")
//                        ->setAudience("all")
//                        ->iosNotification($iosAlert,$ios)
//                        ->androidNotification($content,$android)
//                        ->send();
                }
            }

            //写日志
            if(!empty($alias)) {
                $message = self::log($title, $content, $alias, $param);
                if ($message === false) {
                    return true;
                }
            }else{
                $message = self::log($title, $content, null, $param);
                if ($message === false) {
                    return true;
                }
            }

            //返回结果
            return true;
        } catch (APIConnectionException $e) {
            //写日志
            if(!empty($alias)) {
                $message = self::log($title, $content, $alias, $param);
                if ($message === false) {
                    return "发送推送消息失败！";
                }
            }

            //返回异常信息
            return $e->getMessage();
        } catch (APIRequestException $e) {
            //写日志
            if(!empty($alias)) {
                $message = self::log($title, $content, $alias, $param);
                if ($message === false) {
                    return "发送推送消息失败！";
                }
            }

            //返回异常信息
            return $e->getMessage();
        }
	}

    /*
     * @功能 日志
     * @param string $title 标题
     * @param string $content 内容
     * @param string $alias 别名
     * @开发者：zc
     * @return bool
     */
    public static function log($title,$content,$alias,$param){
        //判断参数类型
        if(is_array($param)){
            $param = json_encode($param);
        }

        //整理数据
        $push_data['customer_id'] = $alias;
        $push_data['message_title'] = $title;
        $push_data['message_content'] = $content;
        $push_data['param'] = $param;
        $push_data['create_time'] = get_date_time();

        //写数据
        $message_add = IDb::getInstance("com_message")->setDbData($push_data)->add();
        if($message_add === false){
            return false;
        }

        //返回值
        return true;
    }
}